package com.cheer.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cheer.dao.OaIntegralDao;
import com.cheer.dao.OaIntegralDetailDao;
import com.cheer.entity.OaIntegralDetailEntity;
import com.cheer.entity.OaIntegralEntity;
import com.cheer.enums.OaIntegralRuleEnum;
import com.cheer.service.OaIntegralService;
import com.cheer.service.OaUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.concurrent.TimeUnit;

@Service("oaIntegralDetailService")
public class OaIntegralServiceImpl extends ServiceImpl<OaIntegralDao, OaIntegralEntity> implements OaIntegralService {

    @Autowired
    private OaIntegralDao oaIntegralDao;

    @Autowired
    private OaIntegralDetailDao oaIntegralDetailDao;

    @Autowired
    private OaUserService oaUserService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void add(String openid) {

        Integer oaUserId = oaUserService.getIdByOpenid(openid);
        if(oaUserId == null){
            return;
        }
        OaIntegralRuleEnum integralRuleEnum = OaIntegralRuleEnum.MATCH;
        Integer type = integralRuleEnum.getCode();
        String typeStr = String.valueOf(integralRuleEnum.getCode());
        Integer integral = integralRuleEnum.getIntegral();
        String currentSecond = String.valueOf(DateUtil.currentSeconds());

        DateTime start = DateUtil.beginOfDay(new Date());
        DateTime end = DateUtil.endOfDay(new Date());
        // 两次一次5积分
        if( oaIntegralDao.selectCountByOpenid(openid, type, start, end) >= 2){
            return;
        }
        // 查询日期
        oaIntegralDao.insert(new OaIntegralEntity(type, integral, openid, null));
        OaIntegralDetailEntity insertDetail = new OaIntegralDetailEntity()
                .setIntegral(integral)
                .setType(typeStr)
                .setOpenid(openid)
                .setStatus(1)
                .setAddTime(currentSecond);
        oaIntegralDetailDao.insert(insertDetail);
        oaUserService.addCredit(oaUserId, integralRuleEnum.getIntegral());
    }

    @Override
    public void updateYearCatch(Integer oaUserId, Integer addCredit) {
        String redisKey = getCreditYearKey(oaUserId, new Date());
        String creditYear = stringRedisTemplate.boundValueOps(redisKey).get();
        if(StrUtil.isNotBlank(creditYear)){
            Integer creditAddResult =  Integer.valueOf(creditYear) + addCredit;
            stringRedisTemplate.opsForValue().set( redisKey, String.valueOf(creditAddResult), 30, TimeUnit.DAYS );
        }

    }

    /**
     * 获取积分Key
     * @param userId
     * @param date
     * @return
     */
    public String getCreditYearKey(Integer userId, Date date){
        return userId+"_credit_"+DateUtil.format(date, "yyyy");
    }
}